package com.xzj.admin.analysis.base.manage;

import com.xzj.admin.analysis.base.common.handle.CommonAbstractAnalysisHandle;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.custom.common.DefaultAnalysisHandle;
import com.xzj.admin.analysis.base.custom.ct.GzAnalysisHandle;
import com.xzj.admin.analysis.base.custom.ct.NmAnalysisHandle;
import com.xzj.admin.analysis.base.custom.it.CasAnalysisHandle;
import com.xzj.admin.analysis.base.custom.it.FcAnalysisHandle;
import com.xzj.admin.analysis.base.custom.it.VSuiteAnalysisHandle;
import com.xzj.admin.analysis.base.custom.it.VcAnalysisHandle;
import com.xzj.admin.analysis.base.custom.ty.YcYcAnalysisHandle;
import com.xzj.admin.analysis.base.custom.ty.ZyyAnalysisHandle;
import com.xzj.admin.analysis.base.function.AnalysisHandleFunction;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: xzj-admin
 * @ClassName: MessageAnalysisFactory
 * @description: 消息解析工厂类
 * @author: XiaZunJun
 * @create: 2024-11-11 16:11
 */
public class MessageAnalysisFactory {
    private static final Map<String, AnalysisHandleFunction> ANALYSIS_CREATE_MAP = new ConcurrentHashMap<>();

    private static final Map<String, CommonAbstractAnalysisHandle> ANALYSIS_HANDLE_MAP = new ConcurrentHashMap<>();

    static {
        ANALYSIS_CREATE_MAP.put(getAnalysisKey(CommonEnums.CLOUD_ID.ITY.getValue(), CommonEnums.CLOUD_TYPE.CAS.name()), CasAnalysisHandle::new);
        ANALYSIS_CREATE_MAP.put(getAnalysisKey(CommonEnums.CLOUD_ID.ITY.getValue(), CommonEnums.CLOUD_TYPE.FC.name()), FcAnalysisHandle::new);
        ANALYSIS_CREATE_MAP.put(getAnalysisKey(CommonEnums.CLOUD_ID.ITY.getValue(), CommonEnums.CLOUD_TYPE.VC.name()), VcAnalysisHandle::new);
        ANALYSIS_CREATE_MAP.put(getAnalysisKey(CommonEnums.CLOUD_ID.ITY.getValue(), CommonEnums.CLOUD_TYPE.VSUITE.name()), VSuiteAnalysisHandle::new);
        ANALYSIS_CREATE_MAP.put(getAnalysisKey(CommonEnums.CLOUD_ID.TYY.getValue(), CommonEnums.CLOUD_TYPE.ZYY.name()), ZyyAnalysisHandle::new);
        ANALYSIS_CREATE_MAP.put(getAnalysisKey(CommonEnums.CLOUD_ID.YCYC.getValue(), CommonEnums.CLOUD_TYPE.YCYC.name()), YcYcAnalysisHandle::new);
        ANALYSIS_CREATE_MAP.put(getAnalysisKey(CommonEnums.CLOUD_ID.CTY.getValue(), CommonEnums.CLOUD_TYPE.DEFAULT.name()), NmAnalysisHandle::new);
        ANALYSIS_CREATE_MAP.put(getAnalysisKey(CommonEnums.CLOUD_ID.CTY.getValue(), CommonEnums.CLOUD_TYPE.GZ.name()), GzAnalysisHandle::new);
    }

    /**
     * 获取消息解析对象
     *
     * @author XiaZunJun
     * @date 2024/11/12 下午10:57
     * @param cloudId 云资源Id
     * @param cloudType 云资源类型
     * * @return CommonAbstractAnalysisHandle
     */
    public static CommonAbstractAnalysisHandle analysisHandle(String cloudId, String cloudType) {
        String handleKey = AnalysisCommonUtil.commonBuildStr(cloudId, cloudType);
        return ANALYSIS_HANDLE_MAP.get(handleKey);
    }

    /**
     * 创建消息解析对象
     *
     * @author XiaZunJun
     * @date 2024/11/12 下午10:55
     * @param cloudId 云资源Id
     * @param cloudType 云资源类型
     *
     */
    public static void createHandle(String cloudId, String cloudType) {
        String handleKey = AnalysisCommonUtil.commonBuildStr(cloudId, cloudType);
        if (!ANALYSIS_HANDLE_MAP.containsKey(handleKey)) {
            AnalysisHandleFunction analysisHandleFunction = ANALYSIS_CREATE_MAP.get(handleKey);
            CommonAbstractAnalysisHandle analysisHandle;
            if(null == analysisHandleFunction){
                analysisHandle = new DefaultAnalysisHandle(cloudId, cloudType);
            }else{
                analysisHandle = analysisHandleFunction.getAnalysis(cloudId, cloudType);
            }
            ANALYSIS_HANDLE_MAP.put(handleKey, analysisHandle);
        }
    }

    private static String getAnalysisKey(String cloudId, String cloudType) {
        return AnalysisCommonUtil.commonBuildStr(cloudId, cloudType);
    }
}
